package com.wjj.sixinone.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wjj.sixinone.common.enums.ExceptionEnum;
import com.wjj.sixinone.common.exception.GlobalException;
import com.wjj.sixinone.common.pojo.entity.Drug;
import com.wjj.sixinone.common.pojo.entity.Employee;
import com.wjj.sixinone.common.pojo.entity.Instrument;
import com.wjj.sixinone.common.pojo.entity.User;
import com.wjj.sixinone.common.util.Result;
import com.wjj.sixinone.mapper.UserMapper;
import com.wjj.sixinone.service.DrugService;
import com.wjj.sixinone.service.EmployeeService;
import com.wjj.sixinone.service.FileUploadService;
import com.wjj.sixinone.service.InstrumentService;
import com.wjj.sixinone.service.minio.MinioFileService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
@AllArgsConstructor
@Service
public class FileUploadServiceImpl implements FileUploadService {

    private final MinioFileService minioFileService;
    private final UserMapper userMapper;
    private final DrugService drugService;
    private final InstrumentService instrumentService;
    private final EmployeeService employeeService;
    @Override
    public Result fileUpload(MultipartFile file,Integer id) {

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<User> eq = wrapper.select(User::getAvatar,User::getId).eq(User::getId, id);
        User user = userMapper.selectOne(eq);
        if (user!=null){
            String avatar = user.getAvatar();

            String url = upload(file,avatar);

            user.setAvatar(url);
            userMapper.update(user,eq);
        }

        return Result.ok();
    }

    @Override
    public Result drugUpload(MultipartFile file, Integer id) {

        LambdaQueryWrapper<Drug> eq = new LambdaQueryWrapper<Drug>().select(Drug::getImg,Drug::getId).eq(Drug::getId, id);
        Drug drug= drugService.getOne(eq);

        if (drug!=null){
            String url = upload(file,drug.getImg());
            drug.setImg(url);
            drugService.update(drug,eq);
        }

        return Result.ok();
    }

    @Override
    public Result instrumentUpload(MultipartFile file, Integer id) {
        LambdaQueryWrapper<Instrument> eq = new LambdaQueryWrapper<Instrument>().select(Instrument::getImg,Instrument::getId).eq(Instrument::getId, id);
        Instrument instrument =  instrumentService.getOne(eq);


        if (instrument!=null){
            String url = upload(file,instrument.getImg());

            instrument.setImg(url);
            instrumentService.updateById(instrument);
        }



        return Result.ok();


    }

    @Override
    public Result employeeUpload(MultipartFile file, Integer id) {
        LambdaQueryWrapper<Employee> eq = new LambdaQueryWrapper<Employee>().select(Employee::getImg, Employee::getId).eq(Employee::getId, id);
        Employee one = employeeService.getOne(eq);
        if (one!=null){
            String url = upload(file,one.getImg());
            one.setImg(url);
            employeeService.updateById(one);
        }
        return Result.ok();
    }

    private String upload(MultipartFile file,String img){
        String url = minioFileService.uploadFile(file);
        if (url == null){
            throw new GlobalException(ExceptionEnum.ERROR_MINIO_SEND);
        }
        if (img!=null){
            minioFileService.deleteFileFromMinio(img);
        }
        return url;
    }

}

